package com.breezelin.freshmarket.utils;

/**
 * Created by Breeze Lin
 * 2016/5/3 13:34
 * breezesummerlin@163.com
 */

import android.app.Activity;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

import com.breezelin.freshmarket.app.Constants;
import com.umeng.analytics.MobclickAgent;
import com.umeng.analytics.social.UMPlatformData;
import com.umeng.socialize.Config;
import com.umeng.socialize.PlatformConfig;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.utils.Log;

/**
 * 友盟的工具类（分享与分享统计）
 */
public class UmengUtils {

	/**
	 * 分享介质——无
	 */
	public static final int SHARE_MEDIA_NONE = -1;
	/**
	 * 分享介质——微博
	 */
	public static final int SHARE_MEDIA_WEIBO = 0;
	/**
	 * 分享介质——微信
	 */
	public static final int SHARE_MEDIA_WECHAT = 1;
	/**
	 * 分享介质——朋友圈
	 */
	public static final int SHARE_MEDIA_CIRCLE = 2;
	/**
	 * 分享介质——qq
	 */
	public static final int SHARE_MEDIA_QQ = 3;
	/**
	 * 分享介质——qq空间
	 */
	public static final int SHARE_MEDIA_QZONE = 4;
	/**
	 * 分享介质——复制
	 */
	public static final int SHARE_MEDIA_COPY = 5;

	/**
	 * 监听
	 */
	private static UMListener listener;

	/**
	 * 单点登录回调
	 */
	public static void ssoCallback(Activity activity, int requestCode, int resultCode, Intent data) {
		UMShareAPI.get(activity).onActivityResult(requestCode, resultCode, data);
	}

	/**
	 * 友盟平台初始化
	 */
	public static void initPlatforms() {
		// 微信 appid appsecret
		PlatformConfig.setWeixin(Constants.WECHAT_APP_ID, Constants.WECHAT_APP_SECRET);
		// 新浪微博 appkey appsecret
		PlatformConfig.setSinaWeibo(Constants.WEIBO_APP_ID, Constants.WEIBO_APP_SECRET);
		// QQ和Qzone appid appkey
		PlatformConfig.setQQZone(Constants.QQ_APP_ID, Constants.QQ_APP_SECRET);

		// 关闭友盟分享的log
		Log.LOG = true;
		// 关闭友盟分享的弹出提示
		Config.IsToastTip = false;
	}

	/**
	 * 进行分享
	 *
	 * @param activity   activity，用于应用程序相关操作
	 * @param media      分享介质
	 * @param title      标题
	 * @param content    内容
	 * @param cover      封面
	 * @param contentUrl 链接
	 */
	public static void share(Activity activity, int media, String title, String content, String cover,
	                         String contentUrl) {
		share(activity, media, title, content, cover, contentUrl, listener);
	}

	/**
	 * 进行分享
	 *
	 * @param activity   activity，用于应用程序相关操作
	 * @param media      分享介质
	 * @param title      标题
	 * @param content    内容
	 * @param cover      封面
	 * @param contentUrl 链接
	 * @param listener   监听（设置一次，生效一次）
	 */
	public static void share(Activity activity, int media, String title, String content, String cover,
	                         String contentUrl, UMListener listener) {
		UmengUtils.listener = listener;
		switch (media) {
			case SHARE_MEDIA_CIRCLE:
				shareOnCircle(activity, title, content, cover, contentUrl);
				break;
			case SHARE_MEDIA_COPY:
				copyToClipboard(activity, contentUrl);
				break;
			case SHARE_MEDIA_QQ:
				shareOnQQ(activity, title, content, cover, contentUrl);
				break;
			case SHARE_MEDIA_QZONE:
				shareOnQzone(activity, title, content, cover, contentUrl);
				break;
			case SHARE_MEDIA_WECHAT:
				shareOnWeChat(activity, title, content, cover, contentUrl);
				break;
			case SHARE_MEDIA_WEIBO:
				shareOnWeibo(activity, title, content, null, cover, contentUrl);
				break;
		}
	}

	// 微博分享
	public static void shareOnWeibo(Activity activity, String title, String content,
	                                String weiboContent, String cover, String contentUrl) {
		ShareAction shareAction = new ShareAction(activity).setPlatform(SHARE_MEDIA.SINA)
				.setCallback(getShareListener(activity));
		if (weiboContent != null && weiboContent.length() > 0) {
			shareAction = shareAction.withText(weiboContent + " " + contentUrl);
		} else {
			shareAction = shareAction.withText(title + " " + content + " " + contentUrl);
		}
		shareAction.withMedia(new UMImage(activity, cover)).share();
	}

	// 微信分享
	private static void shareOnWeChat(Activity activity, String title, String content, String cover,
	                                  String contentUrl) {
		new ShareAction(activity)
				.setPlatform(SHARE_MEDIA.WEIXIN)
				.setCallback(getShareListener(activity))
				.withTitle(title)
				.withText(content)
				.withTargetUrl(contentUrl)
				.withMedia(new UMImage(activity, cover))
				.share();
	}

	// 朋友圈分享
	private static void shareOnCircle(Activity activity, String title,
	                                  String content, String cover, String contentUrl) {
		new ShareAction(activity)
				.setPlatform(SHARE_MEDIA.WEIXIN_CIRCLE)
				.setCallback(getShareListener(activity))
				.withTitle(title)
				.withText(content)
				.withTargetUrl(contentUrl)
				.withMedia(new UMImage(activity, cover))
				.share();
	}

	// qq分享
	private static void shareOnQQ(Activity activity, String title,
	                              String content, String cover, String contentUrl) {
		new ShareAction(activity)
				.setPlatform(SHARE_MEDIA.QQ)
				.setCallback(getShareListener(activity))
				.withTitle(title)
				.withText(content)
				.withTargetUrl(contentUrl)
				.withMedia(new UMImage(activity, cover))
				.share();
	}

	// qq空间分享
	private static void shareOnQzone(Activity activity, String title,
	                                 String content, String cover, String contentUrl) {
		new ShareAction(activity)
				.setPlatform(SHARE_MEDIA.QZONE)
				.setCallback(getShareListener(activity))
				.withTitle(title)
				.withText(content)
				.withTargetUrl(contentUrl)
				.withMedia(new UMImage(activity, cover))
				.share();
	}

	// 复制到剪贴板
	private static void copyToClipboard(Activity activity, String content) {
		// 拷贝到剪贴板
		ClipboardManager clipboardManager =
				(ClipboardManager) activity.getSystemService(Context.CLIPBOARD_SERVICE);
		//noinspection deprecation
		clipboardManager.setText(content);
		Toast.makeText(activity, "已复制链接", Toast.LENGTH_SHORT).show();
	}

	// 获取监听
	private static UMShareListener getShareListener(final Activity activity) {
		return new UMShareListener() {
			@Override
			public void onResult(SHARE_MEDIA platform) {
				Toast.makeText(activity, platform + " 分享成功啦", Toast.LENGTH_SHORT).show();
				statistics(activity, platform);
				if (listener != null) {
					listener.onShareSuccess();
					listener = null;
				}
			}

			@Override
			public void onError(SHARE_MEDIA platform, Throwable t) {
				Toast.makeText(activity, platform + " 分享失败啦", Toast.LENGTH_SHORT).show();
				if (listener != null) {
					listener.onShareFailed();
					listener = null;
				}
			}

			@Override
			public void onCancel(SHARE_MEDIA platform) {
				Toast.makeText(activity, platform + " 分享取消了", Toast.LENGTH_SHORT).show();
			}
		};
	}

	// 分享统计处理
	private static void statistics(Activity activity, SHARE_MEDIA platform) {
		UMPlatformData.UMedia uMedia = UMPlatformData.UMedia.WEIXIN_FRIENDS;
		switch (platform) {
			case WEIXIN:
				uMedia = UMPlatformData.UMedia.WEIXIN_FRIENDS;
				break;
			case WEIXIN_CIRCLE:
				uMedia = UMPlatformData.UMedia.WEIXIN_CIRCLE;
				break;
			case SINA:
				uMedia = UMPlatformData.UMedia.SINA_WEIBO;
				break;
			case QQ:
				uMedia = UMPlatformData.UMedia.TENCENT_QQ;
				break;
			case QZONE:
				uMedia = UMPlatformData.UMedia.TENCENT_QZONE;
				break;
		}

		String user_id = "null";
		UMPlatformData.GENDER gender = UMPlatformData.GENDER.FEMALE;
		UMPlatformData umPlatformData = new UMPlatformData(uMedia, user_id);
		umPlatformData.setGender(gender); //optional
		MobclickAgent.onSocialEvent(activity, umPlatformData);
	}

	/**
	 * 分享监听
	 */
	public interface UMListener {

		/**
		 * 分享成功回调
		 */
		void onShareSuccess();

		/**
		 * 分享失败回调
		 */
		void onShareFailed();
	}
}
